use datasource_connector::connector::SimpleDataSourceConnector;
use model_graph_types::{
    assistant::model::{Provider, ProviderBuilder},
    datasource::{DataSourceConnector, QueryCondition},
    modeling::{
        assistant::{ModelProvider, ModelProviderSetting, ModelProviderSettingBuilder},
        system_meta_database, ClassGetter, Value,
    },
};

pub fn get_providers() -> anyhow::Result<Vec<Provider>> {
    Err(anyhow::anyhow!(""))
}

pub async fn get_provider(
    provider: &String,
) -> anyhow::Result<(ModelProvider, ModelProviderSetting)> {
    let dsc = SimpleDataSourceConnector::default();

    let provide_value: Value = dsc
        .instance_query_one(
            &Some(system_meta_database()?),
            &system_meta_database()?,
            &ModelProvider::get_identifier()?,
            QueryCondition::with_attribute(String::from("name"), provider.clone())?,
            None,
            None,
        )
        .await?;

    let model_provider: ModelProvider = provide_value.try_into()?;

    if let Ok(setting_value) = dsc
        .instance_query_one(
            &Some(system_meta_database()?),
            &system_meta_database()?,
            &ModelProviderSetting::get_identifier()?,
            QueryCondition::with_attribute(
                String::from("provider_identifier"),
                model_provider.identifier.clone(),
            )?,
            None,
            None,
        )
        .await
    {
        let provider_setting: ModelProviderSetting = setting_value.try_into()?;
        //
        tracing::debug!("{:?}|{:?}", model_provider, provider_setting);

        Ok((model_provider, provider_setting))
    } else {
        Ok((
            model_provider,
            ModelProviderSettingBuilder::default().build()?,
        ))
    }
}
